package com.funsport.controller.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funsport.common.Result;
import com.funsport.dto.request.AdminUserCreateRequest;
import com.funsport.dto.request.AdminUserUpdateRequest;
import com.funsport.dto.response.AdminUserResponse;
import com.funsport.service.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * Web管理端 - 后台管理用户Controller
 */
@Slf4j
@Tag(name = "Web管理端后台用户管理", description = "后台管理用户管理相关接口")
@RestController
@RequestMapping("/web/admin-users")
@RequiredArgsConstructor
public class WebAdminUserController {
    
    private final AdminUserService adminUserService;
    
    @Operation(summary = "获取后台管理用户列表")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN')")
    @GetMapping
    public Result<Page<AdminUserResponse>> getAdminUsers(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status) {
        log.info("获取后台管理用户列表: page={}, pageSize={}, keyword={}, status={}", 
            page, pageSize, keyword, status);
        Page<AdminUserResponse> result = adminUserService.getAdminUsers(page, pageSize, keyword, status);
        return Result.success(result);
    }
    
    @Operation(summary = "获取后台管理用户详情")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN')")
    @GetMapping("/{id}")
    public Result<AdminUserResponse> getAdminUserById(@PathVariable Long id) {
        log.info("获取后台管理用户详情: id={}", id);
        AdminUserResponse response = adminUserService.getAdminUserById(id);
        return Result.success(response);
    }
    
    @Operation(summary = "创建后台管理用户")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN')")
    @PostMapping
    public Result<AdminUserResponse> createAdminUser(@Valid @RequestBody AdminUserCreateRequest request) {
        log.info("创建后台管理用户: nickname={}", request.getNickname());
        AdminUserResponse response = adminUserService.createAdminUser(request);
        return Result.success(response);
    }
    
    @Operation(summary = "更新后台管理用户")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @PutMapping("/{id}")
    public Result<AdminUserResponse> updateAdminUser(
            @PathVariable Long id, 
            @Valid @RequestBody AdminUserUpdateRequest request) {
        log.info("更新后台管理用户: id={}", id);
        request.setId(id);
        AdminUserResponse response = adminUserService.updateAdminUser(request);
        return Result.success(response);
    }
    
    @Operation(summary = "删除后台管理用户")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @DeleteMapping("/{id}")
    public Result<String> deleteAdminUser(@PathVariable Long id) {
        log.info("删除后台管理用户: id={}", id);
        adminUserService.deleteAdminUser(id);
        return Result.success("删除成功");
    }
    
    @Operation(summary = "更新后台管理用户状态")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @PostMapping("/{id}/status")
    public Result<String> updateAdminUserStatus(
            @PathVariable Long id,
            @RequestParam String status) {
        log.info("更新后台管理用户状态: id={}, status={}", id, status);
        adminUserService.updateAdminUserStatus(id, status);
        return Result.success("状态更新成功");
    }
    
    @Operation(summary = "重置后台管理用户密码")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @PostMapping("/{id}/reset-password")
    public Result<String> resetAdminUserPassword(
            @PathVariable Long id,
            @RequestParam String newPassword) {
        log.info("重置后台管理用户密码: id={}", id);
        adminUserService.resetAdminUserPassword(id, newPassword);
        return Result.success("密码重置成功");
    }
    
    @Operation(summary = "获取后台管理用户统计数据")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'ADMIN')")
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getAdminUserStatistics() {
        log.info("获取后台管理用户统计数据");
        Map<String, Object> stats = adminUserService.getAdminUserStatistics();
        return Result.success(stats);
    }
}

